package org.cytoscape.myapp.cytoscape_app_v1.internal.visual;

import java.awt.Color;
import java.util.HashMap;

import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.view.presentation.property.BasicVisualLexicon;
import org.cytoscape.view.vizmap.VisualMappingFunction;
import org.cytoscape.view.vizmap.VisualStyle;

public class VisualStyleDefault {
	
	public static String NODE_SIZE = "Node Size";
	public static String NODE_COLOR = "Node Color";
	public static String EDGE_WIDHT = "Edge Widht";
	public static String EDGE_COLOR = "Edge Color";
	public static String NODE_LABEL = "Node Label";
	
	private VisualInstanceDependecies dependecies;
	private VisualStyle style ;
	private HashMap<String, MappingFunction> mappingProperty;
	
	public VisualStyleDefault(VisualInstanceDependecies dependecies) {
		
		this.dependecies = dependecies;
		this.style = dependecies.getVisualStyleFactoryServiceRef().createVisualStyle("Style Default Influence");
		this.dependecies.getVmmServiceRef().setCurrentVisualStyle(this.style);
		this.mappingProperty = new HashMap<String, MappingFunction>();
		this.mappingProperty.put(VisualStyleDefault.NODE_SIZE, new MappingFunctionDiscreteSimple("Social Centrality",this.dependecies,BasicVisualLexicon.NODE_SIZE,Double.class));
		this.mappingProperty.put(VisualStyleDefault.EDGE_WIDHT, new MappingFunctionContinuousSimple("Social Cohesion",this.dependecies,BasicVisualLexicon.EDGE_WIDTH,Double.class));
		this.mappingProperty.put(VisualStyleDefault.EDGE_COLOR, new MappingFunctionContinuousColor("Social Influence",this.dependecies,BasicVisualLexicon.EDGE_STROKE_UNSELECTED_PAINT,Double.class));
		this.mappingProperty.put(VisualStyleDefault.NODE_LABEL, new MappingFunctionPassthroughSimple("shared name", this.dependecies, BasicVisualLexicon.NODE_LABEL, String.class));
		this.dependecies.getVmmServiceRef().addVisualStyle(this.style);
		this.style.setDefaultValue(BasicVisualLexicon.NODE_FILL_COLOR, Color.YELLOW);
	}
	
	public void setVisualMappingFunction(){
		
		for(String mapName: this.mappingProperty.keySet()){
			this.style.addVisualMappingFunction(this.mappingProperty.get(mapName).createVisualMappingFunction());
		}

	}
	
	public void removeVisualMappingFunctions(){	
		this.style.removeVisualMappingFunction(BasicVisualLexicon.EDGE_WIDTH);
		this.style.removeVisualMappingFunction(BasicVisualLexicon.EDGE_STROKE_UNSELECTED_PAINT);
	}
	
	public void setVisualMappingAttribute(String edgeWidth, String edgeColor){
		this.mappingProperty.get(VisualStyleDefault.EDGE_WIDHT).setNameAttribute(edgeWidth);
		this.mappingProperty.get(VisualStyleDefault.EDGE_COLOR).setNameAttribute(edgeColor);
	}
	
	
	public void addPropertyToChange(VisualMappingFunction<?, ?> visualMapping){
		this.style.addVisualMappingFunction(visualMapping);
	}
	
	public void applyVisualStyle(){
		
		this.dependecies.getVmmServiceRef().setCurrentVisualStyle(this.style);
		CyNetworkView myNetworkView = this.dependecies.getApplicationManager().getCurrentNetworkView();
		this.style.apply(myNetworkView);
		myNetworkView.updateView();
	}

	public VisualInstanceDependecies getDependecies() {
		return dependecies;
	}

	public void setDependecies(VisualInstanceDependecies dependecies) {
		this.dependecies = dependecies;
	}

	public VisualStyle getStyle() {
		return style;
	}

	public void setStyle(VisualStyle style) {
		this.style = style;
	}

}
